'use strict';
/* Controllers */
var tplangularControllers = angular.module('tplangularControllers', []);

tplangularControllers.controller('tplangularListCtrl', function ($scope, $http, $location) {
    $scope.status = null;
	$scope.data = null;
    $scope.info = null;
	$scope.hello={info:'hello,world'};
	$scope.refresh = function() {
		 $http.get('json/tplangular_list.do').success(function(data, status) {
		     $scope.tplangularlist = data["dataList"];
	         $scope.status = status;
	         $scope.data = data;
	         $scope.info = data["info"];
	     
		});
	};
	
	$scope.refresh();
    
	//create新建对象
	$scope.newItem = function() {
    	$location.path('/detail/0');
    };
    
    //delete删除
    $scope.removeItem = function(item) {
    	if (item.id > 0)  {
        	console.log("removing item");
	    	$http.post('json/tplangular_delete.do?id='+ item.id, item)
		    	.success(function(data, status, headers, config) {
		            $scope.status = status;
		            $scope.data = data;
		            $scope.info = data["info"];
					$scope.refresh();
		    	}).error(function(data, status, headers, config) {
					$scope.alerts = [
	                	{ type: 'danger', msg: data }
					];				
				});
    	}
    };   
});


//update 编辑页面的保存
tplangularControllers.controller('itemDetailController', function ($scope, $routeParams, $http, $location) {
	if ($routeParams.itemId > 0) {
		$http.get('json/tplangular_load.do?id=' + $routeParams.itemId).success(function(data, status, headers, config) {
		$scope.item =  data["tplangular"];
	    $scope.sexs =
	        [ { key: '男', value: '男'},
	          { key: '女', value: '女'} ];
	
		});
	}	
	$scope.save = function(item) {
		if(item==undefined) alert("不能为空"); //如果是新建，不输入任何值得花，会导致$scope.item为空
		  // alert( $.param(item));
		 	 $http({
			     method  : 'POST',
			     url  : 'json/tplangular_angular_cu.do',	
			     data : $.param(item),  // pass in data as strings
			     headers : { 'Content-Type': 'application/x-www-form-urlencoded' } 
			    })
			    .success(function(data, status, headers, config) {
	  		    	$location.path('/list');
			})
			.error(function(data, status, headers, config) {
				$scope.alerts = [
                	{ type: 'danger', msg: data }
				];				
			});	
		
	};
    
});
